﻿<%@ Page Language="C#" MasterPageFile="~/Common.Master" AutoEventWireup="true" CodeBehind="PLuploads.aspx.cs" Inherits="Web.Views.DataManage.PLuploads.PLuploads" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../../Easy_UI/MyJs/easyui.css"/>
	<link rel="stylesheet" type="text/css" href="../../../Easy_UI/MyJs/icon.css"/>
    <script type="text/javascript" src="../../../Easy_UI/MyJs/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="../../../Easy_UI/MyJs/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../../../Javascript/json2.js"></script>
    <script type="text/javascript" src="../../../Javascript/jquery.filestyle.js"></script>

    <style type="text/css">
        .progressbar-value
        {
	        background-color:LightBlue;
	        border-radius:5px 0 0 5px;
	        width:0;
        }
    </style>

    <script type="text/javascript">
        $(function () {
            $("#selectTemplateFile").filestyle({
                image: "icons/Folder.png",
                imageheight: 24,
                imagewidth: 32,
                width: 200
            });
            $('#msg').dialog({
                buttons: [{
                    text: '取消',
                    iconCls: 'icon-ok',
                    handler: function () {
                        $('#msg').dialog('close');
                    }
                }]
            });
            $('#msg').dialog('close');
        });
    </script>

    <script type="text/javascript">
        var uploadFailFilesList = new Array();

        function selectTemplateFile_onchange(fileInput) {
            if (fileInput != null) {
                var templateFilePath = window.document.getElementById("templateFilePath");
                templateFilePath.value = fileInput.value;
            }
        }

        function btnUploadFile_onclick() {
            var fileType = $('#fileType').val();
            if (fileType == null || fileType == '-1') {
                alert('请选择文件分类');
                return;
            }
            var templateFilePath = window.document.getElementById("templateFilePath");
            if (templateFilePath != null && templateFilePath.value != null) {
                try {
                    var stream = null;
                    var xmlHttp = null;
                    var fileName = "";
                    try {
                        stream = new ActiveXObject('ADODB.Stream');
                    }
                    catch (e) {
                        alert(e.message);
                        return;
                    }
                    if (stream == null) {
                        alert('stream == null');
                        return;
                    }
                    try {
                        var pathSplits = templateFilePath.value.split('\\');
                        fileName = pathSplits[pathSplits.length - 1];
                        stream.Type = 1;
                        stream.Open();
                        stream.LoadFromFile(templateFilePath.value);
                        stream.position = 0;
                    }
                    catch (e) {
                        alert(e.message);
                        return;
                    }
                    try {
                        xmlHttp = new ActiveXObject('Microsoft.XMLHttp');
                        xmlHttp.open('POST', 'TemplateFileHandler.ashx?templateFileName=' + fileName + '&timeStamp=' + new Date().getTime() + '&fileType=' + fileType, false);
                        xmlHttp.onreadystatechange = function () {
                            if (xmlHttp.readyState == 4) {
                                var status = xmlHttp.status;
                                var rstr = xmlHttp.responseText;
                                if (status == '200') {
                                    var objs = eval('(' + rstr + ')');
                                    if (objs.responseCode == 'Template Error') {
                                        $('#errorTemplate').attr('href', 'tempDatas/' + objs.responseObject);
                                        $('#errorTemplate').text(objs.responseObject);
                                        $('#msg').dialog('open');
                                        return;
                                    }
                                    UploadFile(rstr, fileType);
                                }
                                else {
                                    alert("Server Error!");
                                    return;
                                }
                            }
                        }
                        xmlHttp.send(stream.Read());
                    }
                    catch (e) {
                        alert(e.message);
                        return;
                    }
                }
                catch (e) {
                    alert(e.message);
                    return;
                }
            }
            else {
                alert('请先选择上传文件！');
                return;
            }
        }

        function UploadFile(jsonStrPath) {
            $('#logPanel').children().remove();
            uploadFailFilesList.splice(0, uploadFailFilesList.length);
            var objs = eval('(' + jsonStrPath + ')');

            for (i = 0; i < objs.responseObject.length; i++) {
                var div = window.document.createElement('div');
                $(div).attr('id', objs.responseObject[i].AttachFilePath + '_' + objs.responseObject[i].RelatedId);
                var table = window.document.createElement('table');
                $(table).attr('style', 'width:100%;');
                var tr = window.document.createElement('tr');
                var td1 = window.document.createElement('td');
                $(td1).attr('style', 'width:100px;');
                var td2 = window.document.createElement('td');
                var td3 = window.document.createElement('td');
                $(td3).attr('style', 'width:100px;')
                var progressbarDiv = window.document.createElement('div');
                $(progressbarDiv).addClass('easyui-progressbar');
                $(progressbarDiv).progressbar();
                $(tr).append(td1).append(td2).append(td3).appendTo(table);
                $(td2).append(progressbarDiv);
                $(div).append(table);
                $('#logPanel').append(div);
            }

            for (i = 0; i < objs.responseObject.length; i++) {
                var pid = objs.responseObject[i].RelatedId;
                var filePath = objs.responseObject[i].AttachFilePath;
                var fileType = objs.responseObject[i].FileType;
                var fileUploadObj = new FileUploadObj(filePath, pid, fileType, fileUploadCallback);
                fileUploadCallback(filePath, pid, '开始上传', 0, true);
                fileUploadObj.BeginUpLoadFile();
            }
        }

        function fileUploadCallback(filePath, pid, message, precent, flag) {
            var obj = new Object;
            obj.pid = pid;
            obj.filePath = filePath;
            if (flag == false) {
                uploadFailFilesList.push(obj);
            }
            var pathSplits = filePath.split('\\');
            var fileName = pathSplits[pathSplits.length - 1];
            progressbarDiv = window.document.getElementById(filePath + '_' + pid).getElementsByTagName('div')[0];
            $(progressbarDiv).progressbar('setValue', Math.round(precent * 100));
            window.document.getElementById(filePath + '_' + pid).getElementsByTagName('td')[0].innerText = fileName + ':';
            window.document.getElementById(filePath + '_' + pid).getElementsByTagName('td')[2].innerText = message;
        }

        function FileUploadObj(path, pid, fileType,callbackFunc) {
            var obj = new Object;
            obj.xmlHttp = null;
            obj.stream = null;
            obj.filePath = path;
            obj.fileName = "";
            obj.pid = pid;
            obj.fileType = fileType;
            obj.sendCount = 1;
            obj.totalCount = 1;
            obj.fileSize = 0;
            obj.blockSize = 1024 * 1024 * 4;    //每段分为4M
            obj.beginTime = null;
            obj.BeginUpLoadFile = function () {
                try {
                    obj.stream = new ActiveXObject('ADODB.Stream');
                }
                catch (e) {
                    callbackFunc(obj.filePath, obj.pid, e.message, 0, false);
                    return;
                }
                if (obj.stream == null) {
                    callbackFunc(obj.filePath, obj.pid, "不能创建ADODB.Stream对象，请设置浏览器选项！", 0, false);
                    return;
                }
                try {
                    var pathSplits = obj.filePath.split('\\');
                    obj.fileName = pathSplits[pathSplits.length - 1];
                    obj.stream.Type = 1;
                    obj.stream.Open();
                    obj.stream.LoadFromFile(obj.filePath);
                    obj.stream.position = 0;
                    obj.sendCount = 1;
                    obj.fileSize = obj.stream.size;
                    obj.totalCount = Math.ceil(obj.stream.size / obj.blockSize);
                    obj.beginTime = new Date();
                }
                catch (e) {
                    callbackFunc(obj.filePath, obj.pid, e.message, 0, false);
                    return;
                }
                obj.SendData();
                return;
            };
            obj.SendData = function () {
                if (obj.sendCount <= obj.totalCount) {
                    try {
                        obj.xmlHttp = new ActiveXObject('Microsoft.XMLHttp');
                        obj.xmlHttp.open('POST', 'FileUploadHandler.ashx?pid=' + obj.pid + '&sendCount=' + obj.sendCount + '&fileName=' + obj.fileName + '&fileSize=' + obj.fileSize + '&fileType=' + obj.fileType, true);
                        obj.xmlHttp.onreadystatechange = function () {
                            if (obj.xmlHttp.readyState == 4) {
                                var rstr = obj.xmlHttp.responseText;
                                if (rstr == 'OK') {
                                    callbackFunc(obj.filePath, obj.pid, "正在上传", obj.sendCount / obj.totalCount, true);
                                    obj.sendCount++;
                                    obj.SendData();
                                }
                                else {
                                    obj.stream.Close();
                                    callbackFunc(obj.filePath, obj.pid, "Server Error", 0, false);
                                    return;
                                }
                            }
                        }
                        obj.xmlHttp.send(obj.stream.Read(obj.blockSize));
                    }
                    catch (e) {
                        obj.stream.Close();
                        callbackFunc(obj.filePath, obj.pid, e.message, 0, false);
                        return;
                    }
                }
                else {
                    obj.stream.Close();
                    callbackFunc(obj.filePath, obj.pid, "Completed", 1, true);
                    return;
                }
            };
            return obj;
        }
    </script>
 </asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div style="margin:15px 15px 15px 15px;">
        <div style="margin:15px 15px 15px 15px; border:1px solid LightBlue;">
            <div style="margin:5px 5px 5px 5px">
                <span>选择模板：</span>
                <select id="templateType">
                    <option value="1">单井数据</option>
                </select>            
                <a href="UploadTemplate/SingleWellData.xlsx" style="margin-left:5px;">下载模板</a> 
            </div>
            <div style="margin:5px 5px 5px 5px">
                <span>选择文件：</span>
                <input type="text" id="templateFilePath" style="display:none;"/>
                <input type="file" id="selectTemplateFile" onchange="selectTemplateFile_onchange(this);"/>
                <select id="fileType" style=" margin-left:35px">
                    <option value="-1">请选择文件分类...</option>
                    <option value="1">单井数据</option>
                </select>  
                <button type="button" id="btnUpload" class="sexybutton" onclick="btnUploadFile_onclick();" ><span><span><span class="upload">开始上传</span></span></span></button> 
                <button type="button" id="btnRetry" class="sexybutton" onclick="btnRetryUploadFile_onclick();"><span><span><span class="upload">重试</span></span></span></button> 
            </div>
        </div>
        <div style="margin:15px 15px 15px 15px; border:1px solid LightBlue;">
        <div style="margin:5px 5px 5px 5px">
            <span>文件上传进度</span>
        </div>
        <div id="logPanel" style="margin:5px 5px 5px 5px">
        </div>
        </div>
    </div>
    <div id="msg" style="padding:100px 15px 50px 15px ;width:450px;height:250px;">
        <span>模板文件填写有误，请下载<a id="errorTemplate"></a>查看</span>
    </div>
</asp:Content>
